import { defineStore } from "pinia";
import { ref, getCurrentInstance, onMounted } from "vue";
// import socket from "./socket"
// socket()
export const useLoginStore = defineStore("login", () => {
    let menulist = ref([])
    let checkmenu = ref("首页")

    // 获取全局 axios 实例
    const app = getCurrentInstance();
    const axios = app?.proxy?.$axios;



    const upceckmenu = (val) => {
        checkmenu.value = val
        // 存储选中的菜单项到 sessionStorage
        sessionStorage.setItem('checkmenu', val);
    }
    onMounted(() => {
        // 检查 sessionStorage 中是否存有选中的菜单项
        const storedMenu = sessionStorage.getItem('checkmenu');
        if (storedMenu) {
            checkmenu.value = storedMenu; // 恢复选中的菜单
        }
    });

    async function getMenu() {
        const roleid = sessionStorage.getItem('roleid');
        const _id = roleid === "admin" ? "677e329ed5b873118a4dccb8" : "677e3272d5b873118a4dccb6";
        let { data: { data } } = await axios.get("/getmenulist", { params: { _id } })
        let newdata = data.filter(item => item.rank !== 0)
        newdata.sort((a, b) => {
            return a.rank - b.rank
        })
        menulist.value = newdata
        console.log("菜单获取成功",newdata)
    }

    return { menulist, checkmenu, getMenu, upceckmenu }
})